//
//  MyOrdersCtrl.m
//  Traveling
//
//  Created by 曹秀云 on 15/6/5.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "MyOrdersCtrl.h"
#import "MineDataTool.h"
#import "OrderEditResult.h"
#import "OrderItemCell.h"
#import "TravelLineOrderDetailCtrl.h"
#import "ScenicoTicketOrderDetailCtrl.h"
#import "HotelStayOrderDetailCtrl.h"
#import "TravelSpecialtyOrderDetailCtrl.h"

@interface MyOrdersCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property(assign,nonatomic)NSInteger currentType;

@end

@implementation MyOrdersCtrl


-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的订单"];
    [self initWithTableView];
    self.currentType = 2;
    [self.tableView headerBeginRefreshing];
}

-(void)initWithTableView{
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 0)]];
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0 type:self.currentType];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if(self.itemArray.count){
        OrderEditResult *result  = [self.itemArray lastObject];
        maxId =  result.id;
    }
    if(maxId > 0){
        maxId--;
    }
    
    [self requestDataWithMaxId:maxId type:self.currentType];
}

-(void)requestDataWithMaxId:(NSInteger)maxId type:(NSInteger)type{
    [MineDataTool getOrderInfo:type max_id:maxId  success:^(OrderItemRes *result) {
        [Utils refreshComplete:self.tableView];
        if(result.isSucc){
            if(maxId == 0){
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTips:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTips:@"加载失败"];
    }];
}

- (IBAction)titleItemTap:(UIButton *)sender {
    switch (sender.tag) {
        case 1:{
            NSLog(@"旅游新闻");
            self.currentType = sender.tag;
            break;
        }
        case 2:{
            NSLog(@"旅游新闻");
            self.currentType = sender.tag;
            break;
        }
        case 3:{
            NSLog(@"旅游新闻");
            self.currentType = sender.tag;
            break;
        }
        case 4:{
            NSLog(@"旅游新闻");
            self.currentType = sender.tag;
            break;
        }
        default:
            break;
    }
    
    [self.tableView headerBeginRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderItemCell *cell = [OrderItemCell cellWithTableView:tableView];
    [cell initData:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ORDERITEMCELLHEIGHT;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self dealNextCtrl:indexPath.row];
}

-(void)dealNextCtrl:(NSInteger)position{
    switch (self.currentType) {
        case HotelStayOrderDetailType:{
            HotelStayOrderDetailCtrl *ctrl =  [[HotelStayOrderDetailCtrl alloc]initWithOrderSn:[self.itemArray[position] order_sn]];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case ScenicoOrderDetailType:{
            ScenicoTicketOrderDetailCtrl *ctrl =  [[ScenicoTicketOrderDetailCtrl alloc]initWithOrderSn:[self.itemArray[position] order_sn]];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case TravelLineOrderDeatilType:{
            TravelLineOrderDetailCtrl *ctrl =  [[TravelLineOrderDetailCtrl alloc]initWithOrderSn:[self.itemArray[position] order_sn]];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case TravelSpecialtyOrderDetailType:{
            TravelSpecialtyOrderDetailCtrl *ctrl =  [[TravelSpecialtyOrderDetailCtrl alloc]initWithOrderSn:[self.itemArray[position] order_sn]];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        default:
            break;
    }
}


@end
